package com.handpay.nocardpay.baselibrary.setting.view.fragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.download.view.activity.DownloadApkActivity;
import com.handpay.nocardpay.baselibrary.guide.presenter.VersionPresenterImpl;
import com.handpay.nocardpay.baselibrary.guide.view.IVersionDescView;
import com.handpay.nocardpay.baselibrary.guide.view.IVersionView;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.utils.NoCardPayConstant;
import com.handpay.nocardpay.baselibrary.utils.PreferenceManager;
import com.handpay.nocardpay.baselibrary.widget.CustomTextView;

import java.util.HashMap;

/**
 * Created by sxshi on 2017/8/18.
 */

public class AboutFragment extends BaseFragment implements IVersionView, IVersionDescView, View.OnClickListener {
    private VersionPresenterImpl mVersionPresenter;
    private TextView tv_version,tv_appname;
    private CustomTextView ctv_checkversion, ctv_instruction;

    public static AboutFragment newInstance() {

        Bundle args = new Bundle();

        AboutFragment fragment = new AboutFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_about;
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.new_setting_vision_text);
    }

    @Override
    public void initView(View view) {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(toolbar, true);
        tv_version = (TextView) view.findViewById(R.id.tv_version);
        tv_appname  = (TextView) view.findViewById(R.id.tv_appname);
        ctv_checkversion = (CustomTextView) view.findViewById(R.id.ctv_checkversion);
        ctv_instruction = (CustomTextView) view.findViewById(R.id.ctv_instruction);
    }

    @Override
    public void initListener() {
        ctv_checkversion.setOnClickListener(this);
        ctv_instruction.setOnClickListener(this);
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        tv_version.setText("当前版本：" + String.format(_mActivity.getString(R.string.version), ClientEngine.getInstance().VERSION));
        tv_appname.setText(_mActivity.getString(R.string.app_name)+"客户端");
        mVersionPresenter = new VersionPresenterImpl(_mActivity);
        mVersionPresenter.setmVerView(this);
        mVersionPresenter.setmVersionDescView(this);
        boolean isNewVersion = PreferenceManager.getInstance(_mActivity).getBoolean(NoCardPayConstant.IS_LAST_VERSION, true);
        if (isNewVersion) {
            ctv_checkversion.setRightTv(_mActivity.getString(R.string.last_version), null);
//            ctv_checkversion.setLeftTvPaddingRightDrawableVisibility(false);
            ctv_checkversion.setOnClickListener(null);
        } else {
            ctv_checkversion.setRightTv(_mActivity.getString(R.string.find_version_new), null,R.drawable.ic_new);
//            ctv_checkversion.setLeftTvPaddingRightDrawableVisibility(false);
            ctv_checkversion.setOnClickListener(this);
        }
    }

    @Override
    public void lastVersion() {
        DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.last_version));
    }

    @Override
    public void noRemindVersion(String title, String message, final String url, final String version) {
        normalUpdateApk(title, message, url, version);
    }

    @Override
    public void forcedUpdateApk(String title, String message, final String url) {
        DialogUtils.showAlertDialog(_mActivity, title, message, false, getResources().getString(R.string.immediately_update), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                goToDownloadActivity(_mActivity, url);
            }
        });
    }

    @Override
    public void normalUpdateApk(String title, String message, final String url, final String version) {
        DialogUtils.showAlertDialog(_mActivity, title, message, getResources().getString(R.string.immediately_update), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                goToDownloadActivity(_mActivity, url);
            }
        }, getResources().getString(R.string.not_update), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 暂时不更新，但用户可以进入应用，并使用功能
                dialog.dismiss();
                PreferenceManager.getInstance(_mActivity.getApplicationContext()).putBoolean(NoCardPayConstant.IS_NOT_REMIND, false);
            }
        }, getResources().getString(R.string.no_longer_remind), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int arg1) {
                // 用户同样可以进入应用，但是，下次检测版本的时候，如果是同一版本，不再提示，如果有更高版本，会再提示。
                dialog.dismiss();
                PreferenceManager.getInstance(_mActivity.getApplicationContext()).putBoolean(NoCardPayConstant.IS_NOT_REMIND, true);
                PreferenceManager.getInstance(_mActivity.getApplicationContext()).putString(NoCardPayConstant.NOT_REMIND_VERSION, version);
            }
        });
    }

    protected void goToDownloadActivity(Activity activity, String url) {
        Intent intent = new Intent(activity, DownloadApkActivity.class);
        intent.putExtra(DownloadApkActivity.DOWNLOAD_APK_URL, url);
        activity.startActivity(intent);
    }

    @Override
    public void getVersionDesc(String version, String versionDesc) {
        start(VersionIntroductionFragment.newInstance(version, versionDesc));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ctv_checkversion) {
            HashMap<String, Object> paramers = new HashMap<>();
            paramers.put(BaseProtocolKey.VERSION, ClientEngine.getInstance().VERSION);
            paramers.put(BaseProtocolKey.PLATFORM, ClientEngine.getInstance().MOBILE_TYPE);
            paramers.put(BaseProtocolKey.CHANNEL, NoCardPayConfig.CLIENTCHANNEL);
            mVersionPresenter.checkVersion(paramers, true);
        } else if (v.getId() == R.id.ctv_instruction) {
            HashMap<String, Object> params = new HashMap<>();
            params.put(BaseProtocolKey.VERSION, ClientEngine.getInstance().VERSION);
            params.put(BaseProtocolKey.PLATFORM, ClientEngine.getInstance().MOBILE_TYPE);
            params.put(BaseProtocolKey.CHANNEL, NoCardPayConfig.CLIENTCHANNEL);
            mVersionPresenter.getVirsionDesc(params, true);
        }
    }
}
